/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SISAC.DACEA.SERVLETS;

import SISAC.DACEA.BL.SalonBL;
import SISAC.DACEA.ENTITY.Alumno;
import SISAC.DACEA.ENTITY.Salon;
import SISAC.DACEA.ENTITY.Tutor;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

/**
 *
 * @author Abel
 */
public class SSalon extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public SSalon() {
        super();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            /*out.println("<html>");
             out.println("<head>");
             out.println("<title>Servlet SSalon</title>");
             out.println("</head>");
             out.println("<body>");
             out.println("<h1>Servlet SSalon at " + request.getContextPath() + "</h1>");
             out.println("</body>");
             out.println("</html>");*/
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Tutor oTutor = new Tutor();
        oTutor.setCodigoTutor(1);
        oTutor.setNombre("CR7");
        Tutor oTutor1 = new Tutor();
        oTutor1.setCodigoTutor(2);
        oTutor1.setNombre("Huanta");
        Tutor oTutor2 = new Tutor();
        oTutor2.setCodigoTutor(3);
        oTutor2.setNombre("Huantannnn");

        ArrayList<Tutor> lstTutor = new ArrayList<Tutor>();
        lstTutor.add(oTutor);
        lstTutor.add(oTutor1);
        lstTutor.add(oTutor2);
        
        //Tipo de salon
        

        String tutore = new Gson().toJson(lstTutor);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(tutore);                
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // to accept the json data
        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                jb.append(line);
            }
            //System.out.println(jb.toString());
            String datt = String.valueOf(jb);
            jb.toString();
            Salon oSalon = new Salon();
            JSONObject jsonObj = new JSONObject(datt);
            
            String value = (String) jsonObj.get("nombre");
            oSalon.setNombreSalon(value);

            value = (String) jsonObj.get("capacidad");
            oSalon.setCapacidad(Integer.parseInt(value));

            value = (String) jsonObj.get("tutorAsignado");
            oSalon.setCodigoTutor(Integer.parseInt(value));

            value = (String) jsonObj.get("tipoSalon");
            oSalon.setArea(Integer.parseInt(value));

            String json1 = new Gson().toJson("Se creo Correctamente");
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(json1);
            //new SalonBL().crearSalon(oSalon);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
